package cn.iocoder.yudao.module.member.controller.admin.email;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.email.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.email.EmailDO;
import cn.iocoder.yudao.module.member.service.email.EmailService;

@Tag(name = "管理后台 - 用户邮箱")
@RestController
@RequestMapping("/member/email")
@Validated
public class EmailController {

    @Resource
    private EmailService emailService;

    @PostMapping("/create")
    @Operation(summary = "创建用户邮箱")
    @PreAuthorize("@ss.hasPermission('member:email:create')")
    public CommonResult<Long> createEmail(@Valid @RequestBody EmailSaveReqVO createReqVO) {
        return success(emailService.createEmail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户邮箱")
    @PreAuthorize("@ss.hasPermission('member:email:update')")
    public CommonResult<Boolean> updateEmail(@Valid @RequestBody EmailSaveReqVO updateReqVO) {
        emailService.updateEmail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户邮箱")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:email:delete')")
    public CommonResult<Boolean> deleteEmail(@RequestParam("id") Long id) {
        emailService.deleteEmail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户邮箱")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:email:query')")
    public CommonResult<EmailRespVO> getEmail(@RequestParam("id") Long id) {
        EmailDO email = emailService.getEmail(id);
        return success(BeanUtils.toBean(email, EmailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户邮箱分页")
    @PreAuthorize("@ss.hasPermission('member:email:query')")
    public CommonResult<PageResult<EmailRespVO>> getEmailPage(@Valid EmailPageReqVO pageReqVO) {
        PageResult<EmailDO> pageResult = emailService.getEmailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户邮箱 Excel")
    @PreAuthorize("@ss.hasPermission('member:email:export')")
    @OperateLog(type = EXPORT)
    public void exportEmailExcel(@Valid EmailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EmailDO> list = emailService.getEmailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户邮箱.xls", "数据", EmailRespVO.class,
                        BeanUtils.toBean(list, EmailRespVO.class));
    }

}